<?php
namespace app\admin\controller;


use app\admin\common\controller\Base;
use app\admin\common\model\Role as RoleModel;
use think\Exception;
use think\facade\Request;
use app\admin\common\model\Admin;

class Role extends Base
{
    /*
     * 角色列表
     */
    public function roleList(RoleModel $roleModel)
    {
        $datas = $roleModel->getPageList();

        foreach ($datas as $key=>$item) {
            $menuNames = $roleModel->getRoleMenuById($item['id']);
            $userNames = $roleModel->getRoleAdminById($item['id']);
            $datas[$key]['menuNames'] = $menuNames;
            $datas[$key]['userNames'] = $userNames;
        }

        $this->assign('datas',$datas);
        return $this->fetch('roleList');
    }

    /*
     * 角色添加
     */
    public function addRole()
    {
        return $this->fetch('addRole');
    }

    /*
     * 添加角色操作
     */
    public function addRoleDo(RoleModel $roleModel)
    {
        $params = Request::param();
        $rule = 'app\admin\validate\AddRole';
        $result = $this->validate($params,$rule);

        if (true !== $result) {
            $this->error($result);
        }

        $res = $roleModel->addRole($params);

        if ($res) {
            $this->success('添加成功','roleList');
        }

        $this->error('添加失败');
    }

    /*
     * 删除角色操作
     */
    public function delRole(RoleModel $roleModel)
    {
        $id = Request::param('id');

        if (empty($id)) {
            $this->error('数据ID丢失');
        }

        try{
            if ($roleModel->delRole($id)) {
                $this->success('删除成功','roleList');
            }
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }

        $this->error('删除失败');
    }

    /*
     * 角色关联菜单
     */
    public function roleMenu(RoleModel $roleModel)
    {
        $role_id = Request::param('role_id');
        $roleInfo = $roleModel->getInfoById($role_id);

        if (empty($roleInfo)) {
            $this->error('角色数据不存在');
        }

        $hasMenu = $roleModel->getRoleMenu($role_id);

        $this->assign('roleInfo',$roleInfo);
        $this->assign('hasMenu',$hasMenu);

        return $this->fetch('roleMenu');
    }

    /*
     * 角色关联菜单操作
     */
    public function roleMenuDo(RoleModel $roleModel)
    {
        $params = Request::param();
        $menuIds = empty($params['menu_id']) ? [] : $params['menu_id'];
        $roleId = $params['role_id'];
        $roleInfo = $roleModel->getInfoById($roleId);

        if (empty($roleInfo)) {
            $this->error('角色数据不存在');
        }

        $roleModel->updateRoleMenu($roleId,$menuIds);
        $this->success('角色关联菜单成功','roleList');
    }

    /*
     * 角色关联用户
     */
    public function roleAdmin(RoleModel $roleModel, Admin $adminModel)
    {
        $role_id = Request::param('role_id');
        $roleInfo = $roleModel->getInfoById($role_id);

        if (empty($roleInfo)) {
            $this->error('角色数据不存在');
        }

        $hasUser = $roleModel->getRoleAdmin($role_id);
        $datas = $adminModel->getPageList();

        $this->assign('roleInfo',$roleInfo);
        $this->assign('hasUser',$hasUser);
        $this->assign('datas',$datas);

        return $this->fetch('roleAdmin');
    }

    /*
     * 角色关联用户操作
     */
    public function roleAdminDo(RoleModel $roleModel)
    {
        $params = Request::param();
        $userIds = empty($params['user_id']) ? [] : $params['user_id'];
        $roleId = $params['role_id'];
        $roleInfo = $roleModel->getInfoById($roleId);

        if (empty($roleInfo)) {
            $this->error('角色数据不存在');
        }

        $roleModel->updateRoleAdmin($roleId,$userIds);
        $this->success('角色关联用户成功','roleList');
    }
}